﻿using System;
using System.Web.UI.WebControls;
using RazakiBLL;
using RazakiBLL.Managers;
using RazakiSite.UserControls.Forum;

namespace RazakiSite.PagesForum
{
    public partial class MessageHistory : BasePage
    {
        UowRazaki _uowRazaki = new UowRazaki();

        private int MessageId { get { return UtilitiesUI.GetQueryString<int>("id"); } }

        protected void Page_Load(object sender, EventArgs e)
        {
            ConfigControls();

            BindControls();
        }

        protected override void ConfigControls()
        {
            var messageDetail = _uowRazaki.MessageForumRepository.GetWithDetailById(MessageId);
            byte[] picture = !Convert.IsDBNull(messageDetail.Picture) ? messageDetail.Picture : null;
            bool? sex = !Convert.IsDBNull(messageDetail.Sex) ? messageDetail.Sex : null;

            UCMessageQuestion.Image = SetImage(picture, sex);
            UCMessageQuestion.Name = UtilitiesUI.GetFullNameOfUser(messageDetail.UserId);
            UCMessageQuestion.Subject = messageDetail.Subject;
            UCMessageQuestion.Text = messageDetail.Text;
            UCMessageQuestion.Date = UtilitiesBLL.GetPersianDate(messageDetail.CreatedDate, true);

            UCMessageEditReply.TypeId = _uowRazaki.MessageTypeRepository.GetByType(EMessageType.answer.ToString()).Id;           
            if (Page.User.Identity.IsAuthenticated)
            {
                UCMessageEditReply.AllowSubmit = true;
                UCMessageEditReply.PermissionId = _uowRazaki.MessagePermissionRepository.GetByPermission(EMessagePermission.@public.ToString()).Id;
                UCMessageEditReply.MessageId = MessageId;
            }
            else
            {
                UCMessageEditReply.AllowSubmit = false;
                UCMessageEditReply.UrlAndMessage = string.Format("{0}~{1}", Request.Url.PathAndQuery, "برای درج پاسخ");
            }

            //delete

            // کاربری که امکان دیدن دکمه حذف را دارد امکان حذف را نیز خواهد داشت
            if(Page.User.Identity.IsAuthenticated)
            {
                if (UtilitiesUI.IsIdForCurrentUser(messageDetail.UserId) || UtilitiesUI.IsAdmin())
                {
                    MessageActions.ShowDelete = true;
                }
            }
        }

        protected override void BindControls()
        {
            var messageDetailReplyList = _uowRazaki.MessageForumRepository.GetWithDetailByParentId(MessageId);

            RptReply.DataSource = messageDetailReplyList;
            RptReply.DataBind();
        }

        protected void RptReply_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var uCMessageReply = (UCMessage)e.Item.FindControl("UCMessageReply");
                var messageDetail = e.Item.DataItem.ExpandoRecord();
                byte[] picture = !Convert.IsDBNull(messageDetail.Picture) ? messageDetail.Picture : null;
                bool? sex = !Convert.IsDBNull(messageDetail.Sex) ? messageDetail.Sex : null;

                uCMessageReply.Image = SetImage(picture, sex);
                uCMessageReply.Name = UtilitiesUI.GetFullNameOfUser(messageDetail.UserId);
            }
        }

        private string SetImage(byte[] picture, bool? sex)
        {
            string imageUrl;

            if (picture != null)
                imageUrl = UtilitiesUI.ShowImage(picture);
            else if (!sex.HasValue || sex.Value)
                imageUrl = "~/Images/UserMale.png";
            else
                imageUrl = "~/Images/UserFemale.png";

            return imageUrl;
        }

        protected void UCMessageEditReply_DataAdded(object sender, bool added)
        {
            if (added)
                BindControls();
        }
    }
}